package com.gjp.processdemo.config.flowable;

import com.gjp.processdemo.utils.UserUtil;
import org.flowable.idm.api.User;
import org.flowable.ui.common.model.UserRepresentation;
import org.flowable.ui.common.security.DefaultPrivileges;
import org.flowable.ui.common.security.SecurityUtils;
import org.flowable.ui.common.service.exception.NotFoundException;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 郭金鹏
 * @date 2019-12-19 13:25:00
 */
@RestController
@RequestMapping("/app")
public class CustomAccountResource {

    @GetMapping(value = "/rest/account", produces = "application/json")
    public UserRepresentation getAccount() {
        UserRepresentation userRepresentation = new UserRepresentation();
        //获取当前登陆用户
        User user = SecurityUtils.getCurrentUserObject();
        //复制数据
        BeanUtils.copyProperties(user, userRepresentation);
        //设置权限列表
        List<String> authorities = SecurityContextHolder
                .getContext()
                .getAuthentication()
                .getAuthorities().stream()
                .map(GrantedAuthority::getAuthority)
                .collect(Collectors.toList());
        userRepresentation.setPrivileges(authorities);
        //返回
        if (!ObjectUtils.isEmpty(userRepresentation)) {
            return userRepresentation;
        } else {
            throw new NotFoundException();
        }
    }
}
